﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;


namespace pGrid
{
  [Serializable]
  class StationManager
  {
    private List<PowerStation> m_stationStock = new List<PowerStation>();
    private SortedDictionary<int, PowerStation> m_stationsExpected = new SortedDictionary<int, PowerStation>();

    public List<PowerStation> StationStock
    {
      get
      {
        return m_stationStock;
      }
      set
      {
        m_stationStock = value;
      }
    }

    public SortedDictionary<int, PowerStation> StationsExpected
    {
      get
      {
        return m_stationsExpected;
      }
      set
      {
        m_stationsExpected = value;
      }
    }

    public void InitStations(int removeCount)
    {
      List<PowerStation> s_list = new List<PowerStation>();

      s_list.Add(new PowerStation(12, 12, 2, 2, ResourseType.mixed));
      s_list.Add(new PowerStation(14, 14, 2, 2, ResourseType.waste));
      s_list.Add(new PowerStation(15, 15, 2, 3, ResourseType.coal));
      s_list.Add(new PowerStation(16, 16, 2, 3, ResourseType.oil));
      s_list.Add(new PowerStation(17, 17, 1, 2, ResourseType.uranium));
      s_list.Add(new PowerStation(18, 18, 0, 2, ResourseType.wind));
      s_list.Add(new PowerStation(19, 19, 2, 3, ResourseType.waste));
      s_list.Add(new PowerStation(20, 20, 3, 5, ResourseType.coal));
      s_list.Add(new PowerStation(21, 21, 2, 4, ResourseType.mixed));

      s_list.Add(new PowerStation(22, 22, 0, 2, ResourseType.wind));
      s_list.Add(new PowerStation(23, 23, 1, 3, ResourseType.uranium));
      s_list.Add(new PowerStation(24, 24, 2, 4, ResourseType.waste));
      s_list.Add(new PowerStation(25, 25, 2, 5, ResourseType.coal));
      s_list.Add(new PowerStation(26, 26, 2, 5, ResourseType.oil));
      s_list.Add(new PowerStation(27, 27, 0, 3, ResourseType.wind));
      s_list.Add(new PowerStation(28, 28, 1, 4, ResourseType.uranium));
      s_list.Add(new PowerStation(29, 29, 1, 4, ResourseType.mixed));
      s_list.Add(new PowerStation(30, 30, 3, 6, ResourseType.waste));
      s_list.Add(new PowerStation(31, 31, 3, 6, ResourseType.coal));

      s_list.Add(new PowerStation(32, 32, 3, 6, ResourseType.oil));
      s_list.Add(new PowerStation(33, 33, 0, 4, ResourseType.wind));
      s_list.Add(new PowerStation(34, 34, 1, 5, ResourseType.uranium));
      s_list.Add(new PowerStation(35, 35, 1, 5, ResourseType.oil));
      s_list.Add(new PowerStation(36, 36, 3, 7, ResourseType.coal));
      s_list.Add(new PowerStation(37, 37, 0, 4, ResourseType.wind));
      s_list.Add(new PowerStation(38, 38, 3, 7, ResourseType.waste));
      s_list.Add(new PowerStation(39, 39, 1, 6, ResourseType.uranium));
      s_list.Add(new PowerStation(40, 40, 2, 6, ResourseType.oil));
      s_list.Add(new PowerStation(42, 42, 2, 6, ResourseType.coal));

      s_list.Add(new PowerStation(44, 44, 0, 5, ResourseType.wind));
      s_list.Add(new PowerStation(46, 46, 3, 7, ResourseType.mixed));
      s_list.Add(new PowerStation(50, 50, 0, 6, ResourseType.wind));

      m_stationStock.Add(new PowerStation(13, 13, 0, 1, ResourseType.wind));

      System.Random getRand = new Random();
      int pos, size = s_list.Count;
      for (int i = 0; i < size; i++)
      {
        pos = getRand.Next(0, s_list.Count);
        m_stationStock.Add(s_list[pos]);
        s_list.Remove(s_list[pos]);
      }

      for (int i = 0; i < removeCount; i++)
        RandomDelete();

      // добавляются станция 13 и карточка третьего этапа
      m_stationStock.Add(new PowerStation(51, 51, 0, 0, ResourseType.stage3));

      m_stationsExpected.Add(7, new PowerStation(7, 7, 3, 2, ResourseType.oil));
      m_stationsExpected.Add(8, new PowerStation(8, 8, 3, 2, ResourseType.coal));
      m_stationsExpected.Add(9, new PowerStation(9, 9, 1, 1, ResourseType.oil));
      m_stationsExpected.Add(3, new PowerStation(3, 3, 2, 1, ResourseType.oil));
      m_stationsExpected.Add(4, new PowerStation(4, 4, 2, 1, ResourseType.coal));
      m_stationsExpected.Add(5, new PowerStation(5, 5, 2, 1, ResourseType.mixed));
      m_stationsExpected.Add(6, new PowerStation(6, 6, 1, 2, ResourseType.waste));
      m_stationsExpected.Add(10, new PowerStation(10, 10, 2, 2, ResourseType.coal));
      
    }

    public PowerStation GetNewStationFromStock(int pos)
    {
      return this.m_stationStock[pos];
    }
    public void Add(PowerStation item)
    {
      this.m_stationStock.Add(item);
    }
    public void RandomInsert(PowerStation item)
    {
      int size = m_stationStock.Count;
      System.Random getRand = new Random();
      int pos = getRand.Next(0, size);
      this.m_stationStock.Insert(pos, item);
    }
    public void RandomDelete()
    {
      int size = m_stationStock.Count;
      System.Random getRand = new Random();
      int pos = getRand.Next(0, size);
      this.m_stationStock.RemoveAt(pos);
    }
    public void Insert(int position, PowerStation item)
    {
      this.m_stationStock.Insert(position, item);
    }
    public int Size()
    {
      return this.m_stationStock.Count();
    }
    public void InsertIntoExpected(PowerStation st)
    {
      m_stationsExpected.Add(st.Number, st);
    }
    public void DeleteFromExpected(PowerStation st)
    {
      m_stationsExpected.Remove(st.Number);
    }
    public void DeleteFromStock(PowerStation st)
    {
      m_stationStock.Remove(st);
    }
    public void UpdateExpected(PowerStation st_del)
    {
      DeleteFromExpected(st_del);
      PowerStation st = GetNewStationFromStock(0);
      DeleteFromStock(st);
      InsertIntoExpected(st);
    }
  }
}
